package tag.in.activities;

import tag.in.Constants;
import tag.in.R;
import tag.in.TagInApplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

/**
 * Add a Tag
 * 
 * @author andre
 */
public class TagCategoryActivity extends Activity {

    private static final String CLASSTAG = TagCategoryActivity.class.getSimpleName();
    private static final int MENU_FILTER = Menu.FIRST;
    private Spinner category;
    private Button filter_button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(Constants.LOGTAG, " " + TagCategoryActivity.CLASSTAG + " onCreate");

        this.setContentView(R.layout.tag_category);
        
        this.category = (Spinner) findViewById(R.id.category);
        this.filter_button = (Button) findViewById(R.id.category_tag_button);
        
        ArrayAdapter<String> categories = new ArrayAdapter<String>(this, R.layout.spinner_view, getResources()
            .getStringArray(R.array.categories));
        categories.setDropDownViewResource(R.layout.spinner_view_dropdown);
        this.category.setAdapter(categories);
        
        this.filter_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handleFilter();
            }
        });
    }   

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(Constants.LOGTAG, " " + TagCategoryActivity.CLASSTAG + " onResume");
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, TagCategoryActivity.MENU_FILTER, 0, R.string.add_tag_button_label).setIcon(
            android.R.drawable.ic_menu_set_as);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case MENU_FILTER:
                handleFilter();
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
    
    private void handleFilter() {
        TagInApplication application = (TagInApplication) getApplication();
        application.setTagOwner(null);
        application.setTagCategory(this.category.getSelectedItemPosition()+1);
        application.setTagType("category");
        
        // call next Activity, VIEW_LIST
		Intent intent = new Intent(Constants.INTENT_ACTION_VIEW_LIST);
		startActivity(intent);
    }
}
